//
//  EeMsgMark.swift
//  SmoSky
//
//  Created by zhuxietong on 2018/8/3.
//  Copyright © 2018年 tianrun. All rights reserved.
//

import Foundation
import AsyncDisplayKit




public class EeMarkNode:MoDisplayNode
{
    public var markID:String?
    {
        didSet{
            self.checkState()
        }
    }
    public var redNode = ASDisplayNode()
    public var msgType:EeMsgType = EeMsgType(rawValue: "")
    {
        willSet{
            NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: self.msgType.NoticeName), object: nil)
        }
        didSet{
            NotificationCenter.default.addObserver(self, selector: #selector(checkState), name: NSNotification.Name(rawValue: self.msgType.NoticeName), object: nil)
            self.checkState()
        }
    }
    
    public var size = CGSize(width: 8, height: 8)
    {
        didSet{
           
        }
    }
    public required init(size:CGSize)
    {
        super.init()
        self.size = size
        redNode.backgroundColor = UIColor.red
        redNode.alpha = 0.0
        self.backgroundColor = UIColor.clear
        NotificationCenter.default.addObserver(self, selector: #selector(checkState), name: msgType.Notice, object: nil)
    }
    
    public required init() {
        super.init()
        self.size = CGSize(width: 8, height: 8)
        redNode.backgroundColor = UIColor.red
        redNode.alpha = 0.0
        self.backgroundColor = UIColor.clear
        NotificationCenter.default.addObserver(self, selector: #selector(checkState), name: msgType.Notice, object: nil)
    }
    
    public override func didLoad() {
        super.didLoad()
        self.view.bsui.set(radius: size.height/2.0, width: nil, color: nil)
        self.clipsToBounds = true
    }
    
    @objc func checkState()
    {
        if let o_id = self.markID
        {
            redNode.alpha = 0.0
            if !(EeNum.read_state(o_id, type: self.msgType))
            {
                redNode.alpha = 1.0
            }
        }
    }
    
    
    public override func specGetter() -> _Spec {
        return _Spec.wrapper(redNode["\(size.height)",size.width])
    }
    
    deinit
    {
        NotificationCenter.default.removeObserver(self, name:msgType.Notice, object: nil)
    }
}


